<?php
/**
 * Invite(社招人员管理)
 */
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class UserBindComController extends AdminbaseController
{
    //用户模型
    protected $users_model;
    //企业模型
    protected $coms_model;
    //驻厂企业绑定模型
    protected $user_bind_com_model;
    public function _initialize()
    {
        parent::_initialize();
        $this->users_model = D("Common/Users");
        $this->coms_model = D("Common/Coms");
        $this->user_bind_com_model=D("Common/UserBindCom");
    }

    // 后台社招人员列表
    public function index()
    {

        $role_id = I("get.role_id", 0, 'intval');
        $where = array("user_type" => 1, "role_id" => $role_id);
        /**搜索条件**/
        $user_login = I('request.user_login');
        $user_email = trim(I('request.user_email'));
        if ($user_login) {
            $where['user_login'] = array('like', "%$user_login%");
        }
        if ($user_email) {
            $where['user_email'] = array('like', "%$user_email%");;
        }
        $count = $this->users_model->join('__ROLE_USER__ as role ON role.user_id = __USERS__.id')->where($where)->count();
        $page = $this->page($count, 20);
        $users = $this->users_model->join('__ROLE_USER__ as role ON role.user_id = __USERS__.id')
            ->where($where)
            ->order("create_time DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();
        $this->assign("users", $users);
        $this->display();
    }

    // 企业新增页面
    public function add()
    {
        parent::dict_select(array(array(
            'code' => 'COM_PJXZ',
            'name' => 'pay',
            'text' => '平均薪资'
        ), array(
            'code' => 'COM_SSHT',
            'name' => 'trade',
            'text' => '所属行业'
        ), array(
            'code' => 'COM_XZ',
            'name' => 'nature',
            'text' => '企业性质'
        ), array(
            'code' => 'COM_GM',
            'name' => 'scale',
            'text' => '企业规模'
        )
        ));
        $this->display();
    }


    /**
     * 修改用户状态
     * 状态类型参考数字字典USER_SH_STATE
     */
    public function changer_state()
    {
        $id = I("get.id", 0, 'intval');
        $state = I("get.state", 0, 'intval');
        if (isset($state) && $state == 2) {
            $this->invite_model->pass_time = time();
        }
        $this->invite_model->state = $state;
        $rs = $this->invite_model->where('id=' . $id)->save();
        if ($rs !== false) {
            $this->success("成功！");
        } else {
            $this->error("失败！");
        }
    }

    /**
     * 删除社招人员
     * 增加删除时间做软删除
     */
    public function delete()
    {
        $id = I("get.id", 0, 'intval');
        $this->invite_model->deleted_time = time();
        $rs = $this->invite_model->where('id=' . $id)->save(); // 根据条件更新记录
        if ($rs !== false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

    /**
     * 跳转到绑定页面
     */
    public function bind()
    {
        $where = array("com_state" => 1);
        $com_name = I('request.com_name');
        $user_id = I('request.user_id', 0, 'intval');
        $province = I('request.province');
        $city = I('request.city');
        $district = I('request.district');
        if ($com_name) {
            $where['com_name'] = array('like', "%$com_name%");
        }
        if ($province) {
            $where['province'] = $province;
            $this->assign('province', $province);
            if ($city) {
                $where['city'] = $city;
                $this->assign('city', $city);
                if ($district) {
                    $where['district'] = $district;
                    $this->assign('district', $district);
                }
            }
        }
        $count = $this->coms_model->join('as coms LEFT JOIN __USER_COM__ as usercom ON usercom.user_id = coms.id')->where($where)
            ->group('coms.id')
            ->count();
        $page = $this->page($count, 20);
        $list = $this->coms_model->field('coms.id,com_name,province,city,trade,nature,coms.created_time,district,scale,usercom.id as bind_id,user_id')
            ->join('as coms LEFT JOIN __USER_COM__ as usercom ON usercom.com_id = coms.id')
            ->order("coms.created_time DESC")
            ->group('coms.id')
            ->where($where)
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();
        foreach ($list as &$v) {
            if (isset($v['user_id']) && $v['user_id'] == $user_id) {
                $v['is_bind'] = '1';
            } else {
                $v['is_bind'] = null;
            }
        }
        $this->assign('list', $list);
        $this->assign("page", $page->show('Admin'));
        $this->display();

    }

    /**
     * 进行驻厂与用户绑定或者接触绑定
     */
    public function bind_ajax()
    {
        if (IS_AJAX) {
            $r_arr = array('state_code' => 1, 'msg' => '');
            $com_id = I('request.com_id');
            $user_id = I('request.user_id');
            $bind_id = I('request.bind_id'); //中间表ID
            $state = I('request.state');
            switch ($state) {
                case 1://解除绑定 根据id删除
                    if(empty($bind_id)){
                        $delete_where = array(
                            'user_id'=>$user_id,
                            'com_id' => $com_id
                        );
                        if(  $this->user_bind_com_model->where($delete_where)->delete()!=false){
                            $r_arr['state_code'] = 1;
                            $r_arr['msg'] = '解除绑定成功';
                            $r_arr['bind_state'] = 2;
                            $this->ajaxReturn($r_arr);
                        }else{
                            $r_arr['state_code'] = 2;
                            $r_arr['msg'] = '解除绑定失败';
                            $this->ajaxReturn($r_arr);
                        }
                    }else{
                        if($this->user_bind_com_model->delete($bind_id)!=false){
                            $r_arr['state_code'] = 1;
                            $r_arr['msg'] = '解除绑定成功';
                            $r_arr['bind_state'] = 2;
                            $this->ajaxReturn($r_arr);
                        }else{
                            $r_arr['state_code'] = 2;
                            $r_arr['msg'] = '解除绑定失败';
                            $this->ajaxReturn($r_arr);
                        }
                    }
                    break;
                case 2: //进行绑定
                    if ($this->user_bind_com_model->create() !== false) {
                        $user_com_model = array(
                            'com_id'=>$com_id,
                            'user_id'=>$user_id,
                            'created_time'=>time(),
                        );
                         $index =    $this->user_bind_com_model->add($user_com_model) ;
                        if ($index !== false) {
                            $r_arr['state_code'] = 1;
                            $r_arr['msg'] = '绑定成功';
                            $r_arr['bind_state'] = 1;
                            $r_arr['bind_id'] = $index;
                            $this->ajaxReturn($r_arr);
                        } else {
                            $r_arr['state_code'] = 2;
                            $r_arr['msg'] = '绑定失败';
                            $r_arr['bind_state'] = 2;
                            $this->ajaxReturn($r_arr);
                        }
                    } else {
                        $r_arr['state_code'] = 2;
                        $r_arr['msg'] = '绑定失败';
                        $this->ajaxReturn($r_arr);
                    }
                    break;
                default:
                    //没有状态返回
                    $r_arr['state'] = 2;
                    $r_arr['msg'] = '参数错误';
                    $this->ajaxReturn($r_arr);
                    break;
            }
        }

    }
}
